package com.dzqc.fly.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dzqc.fly.db.bo.StudentsFlyMsgBo;
import com.dzqc.fly.db.bo.StudentsYearMsgBo;
import com.dzqc.fly.db.bo.StudentsYearMsgDetailBo;
import com.dzqc.fly.db.dao.SecretaryStudentsDao;
import com.dzqc.fly.db.pojo.SecretaryStudentsPojo;
import com.dzqc.fly.db.pojo.StudentsFlyMsgPojo;
import com.dzqc.fly.db.pojo.StudentsYearMsgDetailPojo;
import com.dzqc.fly.db.vo.SecretaryStudentDetailVo;
import com.dzqc.fly.db.vo.SecretaryStudentsTimeVo;
import com.dzqc.fly.db.vo.SecretarySumVo;
import com.dzqc.fly.service.SecretaryStudentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author hlp
 */
@Service
public class SecretaryStudentsServiceImpl extends ServiceImpl<SecretaryStudentsDao, SecretaryStudentsPojo> implements SecretaryStudentsService {
    @Autowired
    private SecretaryStudentsDao secretaryStudentsDao;

    @Override
    public Page<SecretaryStudentsPojo> searchStudentsList(HashMap param) {
        Page<SecretaryStudentsPojo> page = new Page<>((Integer) param.get("current"),(Integer) param.get("size"));
        List<SecretaryStudentsPojo> pojoList = secretaryStudentsDao.searchStudentsList(page,param);
        page.setRecords(pojoList);
        return page.setRecords(pojoList);
    }

    @Override
    public SecretaryStudentDetailVo searchStudentsDetail(HashMap param) {
        List<StudentsYearMsgDetailPojo> pojoList = secretaryStudentsDao.searchStudentsDetail(param);
        //数据格式转化
        SecretaryStudentDetailVo vo=convert(pojoList);
        return vo;
    }
    private SecretaryStudentDetailVo convert(List<StudentsYearMsgDetailPojo> pojoList){
        if(pojoList==null||pojoList.size()==0){
            return null;
        }
        SecretaryStudentDetailVo secretaryStudentDetailVo=new SecretaryStudentDetailVo();
        //先设置学生基本信息
        StudentsYearMsgDetailPojo baseMsg=pojoList.get(0);
        secretaryStudentDetailVo.setName(baseMsg.getName());
        secretaryStudentDetailVo.setClassId(baseMsg.getClassId());
        secretaryStudentDetailVo.setClassName(baseMsg.getClassName());
        secretaryStudentDetailVo.setIdcard(baseMsg.getIdcard());
        secretaryStudentDetailVo.setTel(baseMsg.getTel());
        secretaryStudentDetailVo.setSno(baseMsg.getSno());
        secretaryStudentDetailVo.setPhoto(baseMsg.getPhoto());

        List<StudentsYearMsgBo> boList=new ArrayList<>();
        //按照年份对学生请假排序
        Map<String, List<StudentsYearMsgDetailPojo>> tempMap = pojoList.stream().collect(Collectors.groupingBy(StudentsYearMsgDetailPojo::getYear));
        for (String key : tempMap.keySet()) {
            List<StudentsYearMsgDetailPojo> studentsYearMsgDetailPojos=tempMap.get(key);
            //设置年份
            StudentsYearMsgBo bo=new StudentsYearMsgBo();
            bo.setYear(key);
           Integer sum=0;
            //设置年份学生请假信息
            List<StudentsYearMsgDetailBo> studentsFlyMsgBos=new ArrayList<>();
            for(StudentsYearMsgDetailPojo s:studentsYearMsgDetailPojos){
                StudentsYearMsgDetailBo detailBo=new StudentsYearMsgDetailBo();
                detailBo.setHours(s.getHours());
                sum=sum+s.getHours();
                detailBo.setReason(s.getReason());
                detailBo.setMonthDay(s.getMonthDay());
                detailBo.setEndTimeMonthDay(s.getEndTimeMonthDay());
                detailBo.setYear(s.getYear());
                studentsFlyMsgBos.add(detailBo);
            }
            //设置总时长
            bo.setYearSum(sum);
            bo.setStudentsFlyMsgBos(studentsFlyMsgBos);
            boList.add(bo);
        }
        secretaryStudentDetailVo.setStudentsYearMsgBos(boList);

        return secretaryStudentDetailVo;
    }

}
